package cn.mjnxin.iot.client;

import java.io.File;
import java.util.Timer;

import com.alibaba.fastjson.parser.ParserConfig;
import com.jfinal.kit.PathKit;
import com.jfinal.server.undertow.PropExt;
import com.jfinal.server.undertow.UndertowServer;

import cn.mjnxin.iot.base.util.MixUtil;
import cn.mjnxin.iot.base.util.WorkFactory;
import cn.mjnxin.iot.client.base.TerminalMessageHandleLoader;
import cn.mjnxin.iot.connector.Connector;
import cn.mjnxin.iot.connector.context.ConnectorContext;
import cn.mjnxin.iot.connector.handle.DefaultMessageHandleFactory;
import cn.mjnxin.iot.connector.message.IMessage;
import cn.mjnxin.iot.connector.message.IMessageSender;
import io.jboot.app.JbootApplication;
import io.jboot.core.listener.JbootAppListenerBase;
import io.undertow.UndertowOptions;

/**
 * 启动器
 * @author CLG 2023年10月10日
 * @version V1.0.0
 */
public class TerminalStart extends JbootAppListenerBase{
    public static final Timer timer = new Timer("Timer_retry_start_stop_service");

    private static String[]  args;
    private static String    terminalAddress;
    
    public static void main(String[] args) throws Exception {
    	TerminalStart.args = args;

		ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
		
        PropExt undertowProp = (new PropExt()).append("jboot.properties");

        String accepts = undertowProp.get("fastjson.accepts");
        for(String accept : accepts.split(",")) {
    		ParserConfig.getGlobalInstance().addAccept(accept);
        }
        
        String host = MixUtil.localIP();
        String port = undertowProp.get("undertow.port");
        
        terminalAddress = host + ":" + port;
        UndertowServer undertowServer = JbootApplication.createServer(args);

        //提前设置webRootPath
        String webRootPath = PathKit.getWebRootPath();
        JbootApplication.start(undertowServer.onStart(builder -> {
            builder.setServerOption(UndertowOptions.MAX_PARAMETERS, undertowProp.getInt("undertow.MAX_PARAMETERS", 1000));
            //最后设置undertow的资源路径
            undertowServer.setResourcePath(webRootPath);
        }));
    }

    public static void startTerminal(String[] args) throws InterruptedException {
        final Connector terminal = (Connector) Connector.me();
        ConnectorContext.CONNECTOR_NAME = "终端";
        ConnectorContext.PEER_NAME = "前置";

        final DefaultMessageHandleFactory messageHandleFactory   = new DefaultMessageHandleFactory(terminal.getContext());
        terminal.init(args, messageHandleFactory, terminalAddress);
        terminal.setContextChangedListener(messageHandleFactory);

        // 反射扫描jar包注册MessageHandle
        TerminalMessageHandleLoader.findAndLoad(messageHandleFactory, WorkFactory.getRootDirectory() + File.separator + "plugins");
        terminal.launch();

        if (terminal.isDevMode()) {
            IMessageSender<? extends IMessage<?>> messageSerder = messageHandleFactory.getMessageHandle(terminal.getPid()).getMessageSerder();
            new Thread(messageSerder, "More_than_MessageSender").start();
        }
    }
    
    @Override
    public void onStartFinish() {
        try {
            startTerminal(args);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
    @Override
    public void onStop() {
    	timer.cancel();
    }
}
